import wxParse from "../../wxParse/wxParse.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile: "",
    codeBtn: "获取验证码",
    isDisabled: false,
    yzmcode:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  mobile: function(e) {
    console.log(e.detail.value)
    this.setData({
      mobile: e.detail.value
    })
  },
  yzmcode:function(e) {
    this.setData({
      yzmcode: e.detail.value
    })
  },
  getCode: function() {
    var tel = Number(this.data.mobile)
    var that = this
    var telRegex = /^1[3|4|5|6|7|8|9]\d{9}$/
    if (telRegex.test(tel)) {
      wx.showLoading({
        title: '发送中,请稍后',
      })
    wx.request({
      url: 'https://miniapp.odeman.com/summercamp/get_mobile_code.php',
      data: {
        mobile:tel
      },
      method:'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success(res) {
        console.log(res.data)
        wx.hideLoading()
        if (res.data.status == "success") {
          wxParse.countDown(that)
          
        }
       
      },
      fail (err) {
        wx.showModal({
          title: '',
          content: '服务器出错请稍后再试',
          success (res) {
            if (res.confirm) {
            } else if (res.cancel) {
            }
          }
        })
      }
    })
  } else {
    wx.showModal({
      title: '',
      content: '请输入正确的手机号',
      success (res) {
        if (res.confirm) {
        } else if (res.cancel) {
        }
      }
    })
  }
  },
  done:function() {
    var _this = this
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: 'https://miniapp.odeman.com/summercamp/useredit.php',
      data: {
        option: 'setmobile',
        userid: wx.getStorageSync('userid'),
        mobile:_this.data.mobile,
        yzmcode: _this.data.yzmcode
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res.data)
        wx.hideLoading()
        if (res.data.status == "error") {
          wx.showModal({
            title: '',
            content: res.data.msg,
            success (res) {
              if (res.confirm) {
             
              } else if (res.cancel) {
             
              }
            }
          })
        } else {
          wx.showToast({
            title: '绑定成功',
            icon: 'success',
            duration: 3000
          })
          setTimeout(function(){
            wx.navigateBack({
              delta: 1
          })
          wx.setStorageSync('mobile',_this.data.mobile)
          },3000)
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})